<?php
$products = array();
$products = $this->getFeaturedProduct();

//Getting Admin Time Zone
$currentTime = Mage_Core_Model_Locale::date(null, null, "en_US", true);

$i = 0;
?>
<h1 class="recenttitle"><?php echo $this->__('Recent Deals'); ?> :</h1><div class="titlebot"></div><br/>
<?php
$resource = Mage::getSingleton('core/resource');
$read = $resource->getConnection('catalog_read');
$orderTable = $resource->getTableName('sales/order');
$city = Mage::getSingleton('core/session')->getCity();
$salesflatorderitem = (string) Mage::getConfig()->getTablePrefix() . 'sales_flat_order_item';
$total_orders = "0";
$id = "0";
$a = 0;
foreach ($products as $_product) {
    if ($_product->getcities() == $city) {
        $a++;
    }
}
$var = $a;
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', '548'); // get the cities attribute id 548
foreach ($attribute->getSource()->getAllOptions(true, true) as $option) {
    $value = $option['value'];
    $cities[$value] = $option['label'];
}

if (count($products) > 0) {
    foreach ($products as $_product) {
        if ($_product->getcities() == $city) {

            //code to check if the deal is current or not, if deal is current, deal will not be displayed, elseif the deal time is over then deal will be displayed
            $ProductToDate = $_product->getResource()->formatDate($_product->getspecial_to_date(), false);
            $ProductFromDate = $_product->getResource()->formatDate($_product->getspecial_from_date(), false);
            $Fromtime = strtotime($ProductFromDate);
            if (strtotime($currentTime) > strtotime($ProductToDate . ' ' . $_product->getTime())) {
?>
<?php
                if (!$_product->isGrouped()) {
                    if ($id == "1") {
                        echo '<div class="clsrecentbox" style="margin:10px 0 0 22px;">';
                    } else {
                        echo '<div class="clsrecentbox">';
                    } $id = "0"; ?>
<?php
                    $current_product = $_product->getID();
                    $todayDate = $_product->getResource()->formatDate($_product->getspecial_from_date(), false);
                    $fromdate = date('F jS, Y', strtotime($todayDate)); //
                    //$this->formatDate($_order->getCreatedAtStoreDate(), 'long'))

                    $discount_price = $_product->getPrice() - $_product->getSpecialPrice();
                    $discount = ($discount_price * 100) / $_product->getPrice();
                    $discount = round($discount);

                    // getting value of base URL to prepare URL, for recent deals view page
                    //starts
                    $baseUrl = Mage::getBaseUrl();
                    $URL = $baseUrl . '?id=' . $current_product . '&amp;recent=1';
                    //ends

                    $dealStatus[0] = "complete";
                    $dealStatus[1] = "processing";
                    $select = $read->select()
                                    ->from(array('cp' => $orderTable))
                                    ->join(array('pei' => $salesflatorderitem), 'pei.order_id=cp.entity_id', array())
                                    ->where('pei.product_id=?', $current_product)
                                    ->where('cp.status IN (?)', $dealStatus);

                    $orders_list = $read->fetchAll($select);
                    $fetch_list = array();
                    $quantity = array();
                    $quantity[0] = "0";

                    //echo $select;
                    if (count($orders_list) > 0) {

                        foreach ($orders_list as $rows) {

                            $order_id = $rows['entity_id'];
                            $order = Mage::getModel('sales/order')->load($order_id);

                            $items = $order->getAllItems();
                            //print_r($items);
                            $itemcount = count($items);
                            $name = array();
                            $unitPrice = array();
                            $sku = array();
                            $ids = array();
                            $qty = array();
                            foreach ($items as $itemId => $item) {
                                if ($current_product == $item->getProductId()) {
                                    $total_orders = $total_orders + 1;
                                    $qty[0] = $item->getQtyOrdered();
                                    $quantity[0] = $quantity[0] + $qty[0];
                                }
                            }
                        }
                    }
?>
                    <h2 class="floatright"><?php echo $this->__('%s', $this->formatDate($_product->getspecial_from_date(), 'long')) ?></h2>
                    <div class="clear"></div>
                    <div class="clsbord">
                        <a href="<?php echo $URL; ?>" style="text-decoration: none;">
                            <img src="<?php echo $this->helper('catalog/image')->init($_product, 'image')->resize(404, 276); ?>" width="178" height="130" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>"/>
                            <h3 align="center"><span><?php echo Mage::helper('core')->currency($_product->getSpecialPrice(), true, false) ?></span></h3><p class="recent-txt"><?php echo substr($this->htmlEscape($_product->getName()), 0, 25) . "..." ?></p>
                            <div class="clsgrid clearfix">
                                <ul class="clsgridul">
                                    <li class="clearfix clsbord-bot"><h5><?php echo $this->__('Value'); ?>:  </h5><h6><?php echo Mage::helper('core')->currency($_product->getPrice(), true, false) ?></h6></li>
                                    <li class="clearfix clsbord-bot"><h5><?php echo $this->__('Savings'); ?>:  </h5><h6><?php echo Mage::helper('core')->currency($discount_price, true, false) ?></h6></li>
                                    <li class="clearfix"><h5><?php echo $this->__('Discount'); ?>:  </h5><h6><?php echo $discount . "%"; ?></h6></li>
                                </ul>
                                <ul class="floatleft dealstatus">
                                    <li>
                                        <p align="center" class="clsratecont">
                                            <span>
<?php
                    if ($_product->gettarget_number() <= $quantity[0]) {
                        echo "<img src='" . Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN) . "frontend/default/inspire-blue/images/Tick_32.png'>";
                    } else {
                        echo "<img src='" . Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN) . "frontend/default/inspire-blue/images/Cross-32.png'>";
                    }
?>
                        </span>
                    <br/><div style="width:50px; font-size: 10px; text-align: center;"><?php
                    if ($_product->gettarget_number() <= $quantity[0]) {
                        echo $this->__('Deal Achieved');
                    } else {
                        echo $this->__('Deal Not Achieved');
                    } ?></div></p></li>
                                </ul>
                            </div>
                        </a>
                    </div>
                    </div>
<?php
                }
                $i = $i + 1;
            }

            if ($i % 3 == '0' || $i == $var) {
                echo "<div class='clear'></div>";
                $id = "1";
            };
        }
    }
} else {
    echo "<h1 class='recenttitle' style='color:#333;text-align:center'>No Recent Deals!</h1>";
}
?>
